package com.amnc.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

import com.amnc.common.dif.DIFSheet;
import com.amnc.common.dif.exception.DIFDataPairParsingException;
import com.amnc.common.dif.exception.DIFKeywordException;
import com.amnc.common.dif.exception.DIFNumberPairInfoException;
import com.amnc.common.dif.exception.DIFStringFormatException;


public class ServiceDIfReader {

	private DIFSheet sheet;

	private int nameIndex;
	
	
	/**
	 * @param sendFilesPath
	 * @param fileName
	 * sendFilesPath: the path of sending dif files;
	 * fileName: the string contained in dif file name ready to be sent;
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws DIFStringFormatException 
	 * @throws DIFNumberPairInfoException 
	 * @throws DIFDataPairParsingException 
	 * @throws DIFKeywordException 
	 */
	public DIFSheet getDifReader(String sendFilesPath,String fileName) throws DIFKeywordException, DIFDataPairParsingException, DIFNumberPairInfoException, DIFStringFormatException, IOException, ParseException{
		File filePath=new File(sendFilesPath); 
		File fileList[] =filePath.listFiles();

		if (filePath.isDirectory()) {
			for (int i = 0; i < fileList.length; i++) {
				nameIndex=fileList[i].toString().indexOf(fileName);
				//specify the dif file rather the processing file
				if (nameIndex!=-1) {
					InputStream inputStream=null;
					try {
						inputStream = new FileInputStream(fileList[i]);
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("new FileInputStream(fileList[i]) go wrong ");
					}
					this.sheet = new DIFSheet(inputStream);
					System.out.println(this.sheet);
					break;
				}
			}
		}else {
			System.out.println("the path of sendFilesPath maybe wrong,please check it!");
		}
		return this.sheet;
		
	}

}
